﻿using Microsoft.VisualBasic.FileIO;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class FileReNameNAction: BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            FileReNameNParam? param = item.ControlParam as FileReNameNParam;
            string watiRenameFile = param.WaitRenameFile;
            string newName = param.NewName;
            string outVarName = param.OutVarName;
            this.GetStringVar(ref watiRenameFile, globleData);
            this.GetStringVar(ref newName, globleData);
            if(!File.Exists(watiRenameFile)){ 
                throw new RpaRuntimeException("需要重命名的文件不存在");
            }
            FileSystem.RenameFile(watiRenameFile, newName);
            string newPath = watiRenameFile.Substring(0, watiRenameFile.LastIndexOf(Path.DirectorySeparatorChar) + 1) + newName;
            this.saveVar(globleData, outVarName, new StringModel(outVarName, newPath), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
